
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="插件名称:" prop="name">
    <el-input v-model="formData.name" :clearable="true" placeholder="请输入插件名称" />
</el-form-item>
        <el-form-item label="插件描述:" prop="description">
    <RichEdit v-model="formData.description"/>
</el-form-item>
        <el-form-item label="插件版本:" prop="version">
    <el-input v-model="formData.version" :clearable="true" placeholder="请输入插件版本" />
</el-form-item>
        <el-form-item label="插件价格:" prop="price">
    <el-input-number v-model="formData.price" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item label="插件状态:" prop="status">
    <el-select v-model="formData.status" placeholder="请选择插件状态" style="width:100%" filterable :clearable="false">
       <el-option v-for="item in []" :key="item" :label="item" :value="item" />
    </el-select>
</el-form-item>
        <el-form-item label="插件图标:" prop="icon">
    <SelectImage
     v-model="formData.icon"
     file-type="image"
    />
</el-form-item>
        <el-form-item label="开发者:" prop="developer">
    <el-input v-model="formData.developer" :clearable="true" placeholder="请输入开发者" />
</el-form-item>
        <el-form-item label="插件分类:" prop="category">
    <el-select v-model="formData.category" placeholder="请选择插件分类" style="width:100%" filterable :clearable="false">
       <el-option v-for="item in []" :key="item" :label="item" :value="item" />
    </el-select>
</el-form-item>
        <el-form-item label="评分:" prop="rating">
    <el-input-number v-model="formData.rating" style="width:100%" :precision="2" :clearable="true" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createPluginInfo,
  updatePluginInfo,
  findPluginInfo
} from '@/api/plugin_market/plugin_info'

defineOptions({
    name: 'PluginInfoForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 图片选择组件
import SelectImage from '@/components/selectImage/selectImage.vue'
// 富文本组件
import RichEdit from '@/components/richtext/rich-edit.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const plugin_statusOptions = ref([])
const plugin_categoryOptions = ref([])
const formData = ref({
            name: '',
            description: '',
            version: '',
            price: 0,
            status: null,
            icon: "",
            developer: '',
            category: null,
            rating: 0,
        })
// 验证规则
const rule = reactive({
               name : [{
                   required: true,
                   message: '请输入插件名称',
                   trigger: ['input','blur'],
               }],
               version : [{
                   required: true,
                   message: '请输入插件版本',
                   trigger: ['input','blur'],
               }],
               price : [{
                   required: true,
                   message: '请输入插件价格',
                   trigger: ['input','blur'],
               }],
               status : [{
                   required: true,
                   message: '请选择插件状态',
                   trigger: ['input','blur'],
               }],
               developer : [{
                   required: true,
                   message: '请输入开发者',
                   trigger: ['input','blur'],
               }],
               category : [{
                   required: true,
                   message: '请选择插件分类',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findPluginInfo({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    plugin_statusOptions.value = await getDictFunc('plugin_status')
    plugin_categoryOptions.value = await getDictFunc('plugin_category')
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createPluginInfo(formData.value)
               break
             case 'update':
               res = await updatePluginInfo(formData.value)
               break
             default:
               res = await createPluginInfo(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
